home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Macintosh Tracker 1.20 / source / Tracker Client Folder / Core 18⁄March⁄1994 / StringUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-24  |  3.9 KB  |  169 lines  |  [TEXT/KAHL]

  1. /* StringUtils.c */
  2.  
  3. #include "StringUtils.h"
  4. #include "Memory.h"
  5. #include "CSack.h"
  6.  
  7.  
  8. static CSack*            StringSack = NIL; /* NIL = operation not in progress */
  9.  
  10.  
  11. void                BeginStringOperation(void)
  12.     {
  13.         ERROR(StringSack != NIL,PRERR(ForceAbort,"BeginStringOperation called "
  14.             "while string operation was in progress"));
  15.         StringSack = new CSack;
  16.         StringSack->ISack(sizeof(Handle),4);
  17.         SetTag((Handle)StringSack,"StringSack");
  18.     }
  19.  
  20.  
  21. void                EndStringOperation(Handle FinalString)
  22.     {
  23.         Handle        Local;
  24.  
  25.         ERROR(StringSack == NIL,PRERR(ForceAbort,"EndStringOperation called "
  26.             "when no string operation was in progress"));
  27.         CheckHandleExistence(FinalString);
  28.      Loop:
  29.         StringSack->ResetScan();
  30.         if (!StringSack->GetNext(&Local))
  31.             {
  32.                 /* done */
  33.                 delete StringSack;
  34.                 StringSack = NIL;
  35.                 return;
  36.             }
  37.         if (Local != FinalString)
  38.             {
  39.                 ReleaseHandle(Local);
  40.             }
  41.         StringSack->KillElement(&Local);
  42.         goto Loop;
  43.     }
  44.  
  45.  
  46. /* concatenate two strings */
  47. Handle            ConStr(Handle LeftStr, Handle RightStr)
  48.     {
  49.         Handle            Temp;
  50.         long                LeftSize;
  51.         long                RightSize;
  52.  
  53.         LeftSize = HandleSize(LeftStr);
  54.         RightSize = HandleSize(RightStr);
  55.         Temp = AllocHandle(LeftSize + RightSize);
  56.         SetTag(Temp,"StringTemp");
  57.         HRNGCHK(Temp,&((*Temp)[0]),LeftSize);
  58.         MemCpy(&((*Temp)[0]),*LeftStr,LeftSize);
  59.         HRNGCHK(Temp,&((*Temp)[LeftSize]),RightSize);
  60.         MemCpy(&((*Temp)[LeftSize]),*RightStr,RightSize);
  61.         RegisterString(Temp);
  62.         return Temp;
  63.     }
  64.  
  65.  
  66. /* take a portion from the middle of the string.  If it would run off */
  67. /* then end, then it is truncated */
  68. Handle            MidStr(Handle String, long Start, long NumChars)
  69.     {
  70.         Handle            Temp;
  71.         long                OldSize;
  72.         long                NewSize;
  73.  
  74.         if (Start < 0)
  75.             {
  76.                 NumChars += Start;
  77.                 Start = 0;
  78.             }
  79.         OldSize = HandleSize(String);
  80.         if ((Start >= OldSize) || (NumChars <= 0))
  81.             {
  82.              ZeroLength:
  83.                 Temp = AllocHandle(0); /* empty string, range started past end */
  84.             }
  85.          else
  86.             {
  87.                 if ((Start + NumChars) - OldSize > 0)
  88.                     {
  89.                         NumChars -= ((Start + NumChars) - OldSize);
  90.                         if (NumChars <= 0)
  91.                             {
  92.                                 goto ZeroLength;
  93.                             }
  94.                     }
  95.                 Temp = AllocHandle(NewSize);
  96.                 HRNGCHK(Temp,*Temp,NewSize);
  97.                 MemCpy(*Temp,&((*String)[Start]),NewSize);
  98.             }
  99.         SetTag(Temp,"StringTemp");
  100.         RegisterString(Temp);
  101.         return Temp;
  102.     }
  103.  
  104.  
  105. /* substitutes the Replacement string into String at the first occurrence */
  106. /* of Key.  If Key is not in String, then String is returned unchanged */
  107. Handle            ReplaceStr(Handle String, Handle Key, Handle Replacement)
  108.     {
  109.         long            Scan;
  110.         long            KeySize;
  111.         long            StringSize;
  112.         long            ReplacementSize;
  113.         Handle        Temp;
  114.  
  115.         KeySize = HandleSize(Key);
  116.         StringSize = HandleSize(String);
  117.         for (Scan = 0; Scan <= StringSize - KeySize; Scan += 1)
  118.             {
  119.                 if (MemEqu(&((*String)[Scan]),*Key,KeySize))
  120.                     {
  121.                         ReplacementSize = HandleSize(Replacement);
  122.                         Temp = AllocHandle(StringSize - KeySize + ReplacementSize);
  123.                         /* copying over preceding characters */
  124.                         HRNGCHK(Temp,&((*Temp)[0]),Scan);
  125.                         MemCpy(&((*Temp)[0]),&((*String)[0]),Scan);
  126.                         /* copying over replacement */
  127.                         HRNGCHK(Temp,&((*Temp)[Scan]),ReplacementSize);
  128.                         MemCpy(&((*Temp)[Scan]),*Replacement,ReplacementSize);
  129.                         /* copying over remainder */
  130.                         HRNGCHK(Temp,&((*Temp)[Scan + ReplacementSize]),StringSize - KeySize - Scan);
  131.                         MemCpy(&((*Temp)[Scan + ReplacementSize]),&((*String)[Scan + KeySize]),
  132.                             StringSize - KeySize - Scan);
  133.                         SetTag(Temp,"StringTemp");
  134.                         RegisterString(Temp);
  135.                         return Temp;
  136.                     }
  137.             }
  138.         return String;
  139.     }
  140.  
  141.  
  142. Handle            CString(char* Start)
  143.     {
  144.         Handle        Temp;
  145.  
  146.         Temp = HandleOf(Start,StrLen(Start));
  147.         SetTag(Temp,"StringTemp");
  148.         RegisterString(Temp);
  149.         return Temp;
  150.     }
  151.  
  152.  
  153. Handle            StringOf(char* Start, long Length)
  154.     {
  155.         Handle        Temp;
  156.  
  157.         Temp = HandleOf(Start,Length);
  158.         SetTag(Temp,"StringTemp");
  159.         RegisterString(Temp);
  160.         return Temp;
  161.     }
  162.  
  163.  
  164. Handle            RegisterString(Handle TheString)
  165.     {
  166.         StringSack->PushElement(&TheString);
  167.         return TheString;
  168.     }
  169.